<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Backend_BrandController
 *
 * @author Eugene
 */
class Backend_BrandController extends Zend_Controller_Action
{

	/**
	 * @var Application_Model_Mapper_Brand
	 */
	protected $_mapper;

	/**
	 *
	 * @return Application_Model_Mapper_Brand
	 */
	public function getMapper()
	{
		if (null === $this->_mapper) {
			$this->_mapper = new Application_Model_Mapper_Brand;
		}

		return $this->_mapper;
	}

	/**
	 *
	 * @return void
	 */
	public function indexAction()
	{
		$messages = $this->_helper->flashMessenger->getMessages();
		$messages = array_flip($messages);

		$this->view->messages = $messages;
		$this->view->entries = $this->getMapper()->fetchAll();
	}

	/**
	 *
	 * @return void
	 */
	public function createAction()
	{
		$form = new Backend_Form_Brand_Create;

		if ($this->getRequest()->isPost()) {
			if ($form->isValid($this->getRequest()->getPost())) {
				$model = new Application_Model_Brand($form->getValues());
				$this->getMapper()->save($model);

				$this->_helper->FlashMessenger('created');
				return $this->_helper->redirector('index');
			}
		}

		$this->view->form = $form;
	}

	/**
	 *
	 * @return void
	 */
	public function updateAction()
	{
		$model = new Application_Model_Brand;
		$form = new Backend_Form_Brand_Update;
		$this->getMapper()->fetchById($this->getRequest()->getParam('id'), $model);

		if ($this->getRequest()->isPost()) {
			if ($form->isValid($this->getRequest()->getPost())) {
				$model->setOptions($form->getValues());
				$this->getMapper()->save($model);

				$this->_helper->flashMessenger('updated');
				return $this->_helper->redirector('index');
			}
		} else {
			$form->populate($model->toArray());
		}

		$this->view->form = $form;
	}

	/**
	 *
	 * @return void
	 */
	public function deleteAction()
	{
		$this->getMapper()->delete($this->getRequest()->getParam('id'));

		$this->_helper->flashMessenger('deleted');
		return $this->_helper->redirector('index');
	}

}